import {ref,computed} from 'vue'
import { defineStore } from 'pinia'
export const useUserStore = defineStore('user',()=>{
    // 从 localStorage 恢复 token 和 adminid
    const token=ref<string>(localStorage.getItem('token') || '')
    const adminid=ref<string>(localStorage.getItem('adminid') || '')
    const role=ref<number>(1)
    function changeRole(newRole:number){
        role.value=newRole
    }
    function changeToken(newToken:string){
        token.value=newToken
        localStorage.setItem('token',newToken)
    }
    function changeAdminId(newAdminId:string){
        adminid.value=newAdminId
        localStorage.setItem('adminid',newAdminId)
    }
    function loginout(){
        localStorage.removeItem('token')
        localStorage.removeItem('adminid')
        token.value=''
        adminid.value=''
    }
    const isLogin=computed<boolean>(()=>token.value?true:false)
    return {
        isLogin,
        loginout,
        changeAdminId,
        changeToken,
        token,
        adminid,
        role,
        changeRole
    }
},
// {
//     persist: true,
//   },
)